﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using GravityBallScreenSaver;
using System.Threading;

namespace GravityBallScreenSaver
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private HwndSource previewScreenContent;
        private GravityBall previewScreen;
        private GravityBall[] displayScreens;

        [STAThread()]
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // Check arguments for startup of screen saver
            // s means start
            // c means config
            // p means preview
            if (e.Args.Length <= 0)
            {
                ShowScreensaver();
                return;
            }

            switch (e.Args[0].ToLower())
            {
                case "/s":
                    ShowScreensaver();
                    break;
                case "/p":
                    if (e.Args.Length > 1)
                        ShowPreview(e.Args[1]);
                    return;
                    break;
                case "/c":
                    ShowConfig();
                    break;
                default:
                    Application.Current.Shutdown();
                    break;
            }
        }

        [STAThread()]
        private void ShowScreensaver()
        {
            System.Windows.Forms.Screen[] screens = System.Windows.Forms.Screen.AllScreens;

            displayScreens = new GravityBall[screens.Length];

            for (int i = 0; i < screens.Length; i++)
            {                
                displayScreens[i] = new GravityBall(screens[i].Bounds.Width, screens[i].Bounds.Height);                
                displayScreens[i].Left = screens[i].Bounds.Left;
                displayScreens[i].Top = screens[i].Bounds.Top;
                displayScreens[i].Width = screens[i].Bounds.Width;
                displayScreens[i].Height = screens[i].Bounds.Height;                
                //displayScreens[i].WindowState = WindowState.Maximized;
                displayScreens[i].Show();
            }
            //Get each screen
            //TODO: Start seperate window maximized to each screen

            //GBSS[] Screens = new GBSS[SCREENS.COUNT]
            //FOR INT I = 0 TO SCREEN.COUNT
            //Screens[i] = new GBSS(Initialize);
            //Screens[i].show
            //Next I
        }

        private void ShowPreview(string handle)
        {
            try
            {
                // Preview mode--display in little window in Screen Saver dialog
                // (Not invoked with Preview button, which runs Screen Saver in
                // normal /s mode).

                previewScreen = new GravityBall();

                Int32 previewHandle = Convert.ToInt32(handle);

                IntPtr pPreviewHnd = new IntPtr(previewHandle);

                RECT lpRect = new RECT();
                bool bGetRect = Win32API.GetClientRect(pPreviewHnd, ref lpRect);

                HwndSourceParameters sourceParams = new HwndSourceParameters("sourceParams");

                sourceParams.PositionX = 0;
                sourceParams.PositionY = 0;
                sourceParams.Height = lpRect.Bottom - lpRect.Top;
                sourceParams.Width = lpRect.Right - lpRect.Left;
                sourceParams.ParentWindow = pPreviewHnd;
                sourceParams.WindowStyle = (int)(WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD | WindowStyles.WS_CLIPCHILDREN);

                previewScreenContent = new HwndSource(sourceParams);
                previewScreenContent.Disposed += new EventHandler(previewScreenContent_Disposed);
                previewScreenContent.RootVisual = previewScreen.grid1;
            }
            catch (Exception)
            {
                //Unable to properly introduce preview for screen saver
                //Let's exit
                Application.Current.Shutdown();
            }
        }

        private void ShowConfig()
        {
            // Config mode, launched from Settings button in screen saver dialog
            Settings settings = new Settings();
            settings.Show();        
        }

        /// <summary>
        /// Event that triggers when parent window is disposed--used when doing
        /// screen saver preview, so that we know when to exit.  If we didn't 
        /// do this, Task Manager would get a new .scr instance every time
        /// we opened Screen Saver dialog or switched dropdown to this saver.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void previewScreenContent_Disposed(object sender, EventArgs e)
        {
            previewScreen.Close();
        }
    }
}
